<template>
  <div class="cart-item">
    <div class="cart-item-header">
      <div class="order-time">
        <i class="iconfont icon-shijian"></i>
        <span>订单时间：{{ cart.createTime }}</span>
      </div>
    </div>
    
    <div class="cart-item-body">
      <div class="product-image-section">
        <router-link :to="'goodview/'+cart.goodId" class="product-link">
          <img :src="baseApi + cart.img" class="product-image" :alt="cart.goodName">
        </router-link>
      </div>
      
      <div class="product-info-section">
        <div class="product-details">
          <div class="detail-row">
            <div class="detail-item">
              <span class="detail-label">商品名称</span>
              <span class="detail-value product-name">{{ cart.goodName }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">规格</span>
              <span class="detail-value">{{ cart.standard }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">单价</span>
              <span class="detail-value price">￥{{ realPrice.toFixed(2) }}</span>
            </div>
            <div class="detail-item">
              <span class="detail-label">数量</span>
              <div class="quantity-control">
                <el-button 
                  class="quantity-btn" 
                  @click="countChangeFlag=true" 
                  v-if="!countChangeFlag"
                >
                  {{ cart.count }}
                </el-button>
                <el-input-number 
                  v-model="cart.count" 
                  :min="1" 
                  :max="cart.store" 
                  v-if="countChangeFlag"
                  class="quantity-input"
                  size="small"
                ></el-input-number>
              </div>
            </div>
            <div class="detail-item">
              <span class="detail-label">总价</span>
              <span class="detail-value total-price">￥{{ totalPrice }}</span>
            </div>
          </div>
        </div>
        
        <div class="action-buttons">
          <el-button 
            type="success" 
            class="pay-btn" 
            @click="pay"
          >
            <i class="iconfont icon-zhifu"></i>
            支付
          </el-button>
          <el-popconfirm
            @confirm="del"
            title="确定删除这个商品吗？"
            confirm-button-text="确定"
            cancel-button-text="取消"
          >
            <el-button 
              type="danger" 
              slot="reference" 
              class="delete-btn"
            >
              <i class="iconfont icon-shanchu"></i>
              移除
            </el-button>
          </el-popconfirm>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: "CartItem",
  props: {
    cart: Object,
    countChangeFlag: false,
  },
  created() {

  },
  mounted() {

  },
  data() {
    return {

      baseApi: this.$store.state.baseApi,
    }
  },
  computed: {
    totalPrice: function () {
      return (this.realPrice * this.cart.count).toFixed(2)
    },
    realPrice: function () {
      return (this.cart.price * this.cart.discount)
    }
  },
  methods: {
    //从购物车移除
    del(id) {
      this.request.delete("/api/cart/" + this.cart.id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.$emit('delete', this.cart.id)
        }
      })
    },
    //跳转到支付页面
    pay() {
      let good = {id: this.cart.goodId, name: this.cart.goodName, imgs: this.cart.img, discount: this.cart.discount}
      this.$router.push({name: 'preOrder',
        query: {
          good: JSON.stringify(good),
          realPrice: this.realPrice,
          num: this.cart.count,
          standard: this.cart.standard,
          cartId: this.cart.id
        }
      })
    },
  }
}
</script>

<style scoped>
.cart-item {
  background: #ffffff;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.cart-item:hover {
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.12);
  transform: translateY(-2px);
}

.cart-item-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 15px 25px;
  color: white;
  position: relative;
}

.cart-item-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.05) 100%);
  pointer-events: none;
}

.order-time {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  position: relative;
  z-index: 1;
}

.order-time i {
  font-size: 16px;
  opacity: 0.9;
}

.cart-item-body {
  padding: 25px;
  display: flex;
  gap: 25px;
  align-items: flex-start;
}

.product-image-section {
  flex-shrink: 0;
}

.product-link {
  display: block;
  text-decoration: none;
}

.product-image {
  width: 120px;
  height: 120px;
  object-fit: cover;
  border-radius: 10px;
  border: 2px solid #f0f0f0;
  transition: all 0.3s ease;
}

.product-link:hover .product-image {
  border-color: #667eea;
  transform: scale(1.05);
}

.product-info-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.product-details {
  flex: 1;
}

.detail-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 20px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.detail-label {
  font-size: 14px;
  color: #666;
  font-weight: 500;
}

.detail-value {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.product-name {
  font-weight: 600;
  color: #2c3e50;
  line-height: 1.4;
}

.price {
  color: #e74c3c;
  font-weight: 600;
}

.total-price {
  color: #e74c3c;
  font-size: 18px;
  font-weight: 700;
}

.quantity-control {
  display: flex;
  align-items: center;
}

.quantity-btn {
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  color: #495057;
  font-weight: 500;
  transition: all 0.3s ease;
}

.quantity-btn:hover {
  background: #667eea;
  border-color: #667eea;
  color: white;
}

.quantity-input {
  width: 120px;
}

.action-buttons {
  display: flex;
  gap: 15px;
  justify-content: flex-end;
  margin-top: 10px;
}

.pay-btn {
  background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
  border: none;
  padding: 10px 20px;
  border-radius: 20px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(40, 167, 69, 0.3);
}

.pay-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(40, 167, 69, 0.4);
  background: linear-gradient(135deg, #218838 0%, #1ea085 100%);
}

.delete-btn {
  background: linear-gradient(135deg, #dc3545 0%, #c82333 100%);
  border: none;
  padding: 10px 20px;
  border-radius: 20px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(220, 53, 69, 0.3);
}

.delete-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(220, 53, 69, 0.4);
  background: linear-gradient(135deg, #c82333 0%, #bd2130 100%);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .cart-item-body {
    flex-direction: column;
    gap: 20px;
  }
  
  .detail-row {
    grid-template-columns: 1fr;
    gap: 15px;
  }
  
  .action-buttons {
    justify-content: center;
    flex-wrap: wrap;
  }
  
  .product-image {
    width: 100px;
    height: 100px;
  }
}
</style>